package com.ruoyi.system.service.module.event.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.QRCodeUtilEx;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.system.domain.module.event.EventPointQrInfo;
import com.ruoyi.system.domain.module.event.EventTerminals;
import com.ruoyi.system.mapper.module.estate.EstateRoomMapper;
import com.ruoyi.system.mapper.module.event.EventPointQrInfoMapper;
import com.ruoyi.system.mapper.module.event.EventTerminalsMapper;
import com.ruoyi.system.service.module.event.IEventTerminalsService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 巡检设备Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-25
 */
@Service
public class EventTerminalsServiceImpl extends ServiceImpl<EventTerminalsMapper, EventTerminals> implements IEventTerminalsService {

	@Value("${ruoyi.profile}")
	private String uploadPath;

	@Value("${ruoyi.imgUrl}")
	private String imgUrl;
	@Resource
	private EventTerminalsMapper eventTerminalsMapper;
	@Resource
	private EventPointQrInfoMapper eventPointQrInfoMapper;

	@Override
	public Integer addBulk(EventTerminals eventTerminals,Integer i) {
		Integer bl = 0;
		eventTerminals.setTerminalName(eventTerminals.getTerminalName()+i);
		eventTerminals.setTerminalNum(eventTerminals.getTerminalNum()+i);
		bl = eventTerminalsMapper.insert(eventTerminals);
		try{
			String fileName = "terminal_qr_" + eventTerminals.getId() + ".jpg";
			String content = "https://www.caihengyun.com/wxmini/terminal/info?id=" + eventTerminals.getId();
			int upload_type = FileUploadUtils.TYPE_TERMINALS;
			System.out.println("content:  "+ content);
			String img_key = FileUploadUtils.getUploadPrefix(upload_type) + fileName;
			QRCodeUtilEx.createQrCodeImg(content, eventTerminals.getTerminalName(), null,
				uploadPath +"/"+img_key, true, fileName, upload_type);
			eventTerminals.setQrCode(img_key);
			if (eventTerminals.getPointQrInfoId()!=null){
				EventPointQrInfo info = eventPointQrInfoMapper.selectById(eventTerminals.getPointQrInfoId());
				if (info!=null){
					eventTerminals.setPointQrId(info.getPointQrId());
				}
			}
			eventTerminalsMapper.updateById(eventTerminals);
		}catch (Exception ex){
			ex.printStackTrace();
		}
		return bl;
	}
}
